﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

using System.Windows.Forms;


namespace GetDeviceInfo
{
    public class PriScr
    {
        [DllImport("User32.dll", EntryPoint = "FindWindow")]
        private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        [DllImport("User32.dll", EntryPoint = "ShowWindow")]   
        private static extern bool ShowWindow(IntPtr hWnd, int type);




        public static void PrintScreen()
        {
            Console.Title = "MyConsoleApp";
            Thread.Sleep(1);
            IntPtr ParenthWnd = new IntPtr(0);
            IntPtr et = new IntPtr(0);
            ParenthWnd = FindWindow(null, "MyConsoleApp");
            ShowWindow(ParenthWnd, 2);//隐藏本dos窗体, 0: 后台执行；1:正常启动；2:最小化到任务栏；3:最大化

            // 等待1秒，确保最小化完成
            Thread.Sleep(1000);

            // 获取屏幕尺寸
            Rectangle bounds = Screen.GetBounds(Point.Empty);
            // 创建一个与屏幕尺寸相同的Bitmap对象
            using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))
            {
                // 创建一个Graphics对象用于绘制
                using (Graphics g = Graphics.FromImage(bitmap))
                {
                    // 将屏幕内容绘制到bitmap上
                    g.CopyFromScreen(Point.Empty, Point.Empty, bounds.Size);
                }

                // 保存图片到桌面，存在则覆盖
                string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
                string screenshotPath = Path.Combine(desktopPath, "Screenshot.png");
                if (File.Exists(screenshotPath))
                {
                    File.Delete(screenshotPath);
                }
                bitmap.Save(screenshotPath, ImageFormat.Png);
            }
            Console.WriteLine("截图已保存到桌面！");

            ShowWindow(ParenthWnd, 1);//隐藏本dos窗体, 0: 后台执行；1:正常启动；2:最小化到任务栏；3:最大化
        }

    }
}
